package com.example.demohello.controller;

import com.example.demohello.entity.Course;
import com.example.demohello.service.CourseService;
import com.example.demohello.vo.CourseRequestBo;
import com.example.demohello.vo.ResultCourseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api")
public class IndexController {



    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Autowired
    JdbcTemplate jdbcTemplate;

    @Autowired
    CourseService courseService;

    @RequestMapping("/")
    public  String  index(){
        return  "index";
    }

    @RequestMapping("/getTestMysql")
    @ResponseBody
    public  String getTestMysql(){
        String sql  = "select * from course";
        List<Map<String, Object>> mylist = jdbcTemplate.queryForList(sql);
        System.out.println(mylist.toString());
        return mylist.toString();
    }


    @RequestMapping("/getTestRedis")
    @ResponseBody
    public  String getTestRedis(){
        redisTemplate.opsForValue().set("test","{course_id=01, course_name=语文, teacher_id=02}");
       // System.out.println(redisTemplate.opsForValue().get("test"));
        return redisTemplate.opsForValue().get("test");
    }

    @RequestMapping("/toAddCourse")
    public  String  toAddCourse(){
        return "course/addCourse";
    }

    @RequestMapping("/addCourse")
    public  String addCourse(Course course){

        courseService.addCourse(course);

        return "redirect:/toCourseList";
    }


    @RequestMapping("/deleteCourse")
    public  String deleteCourse(Integer id){
        String resuleString = "";
        try{
            Integer result = courseService.deleteCourse(id);
            if(result>0)
                resuleString =   "删除成功, 删除ID:  "+id;
            else
                resuleString =   "操作失败";
        }catch (Exception e){
            System.out.println(e.getMessage());
            resuleString =   "操作失败 : "+e.getMessage();
        }
        System.out.println(resuleString);
        return "redirect:/toCourseList";
    }

    @RequestMapping("/toCourseList")
    public String toCourseList(Model model){
        List<Course> courseList  = courseService.selectCourse();
        model.addAttribute("courseList",courseList);
        return "course/courseList";
    }

    @RequestMapping("/toEditCourse")
    public String toEditCourse(Model model,Integer id){
        Course  course  = courseService.selectOneCourse(id);
        model.addAttribute("course",course);
        return "course/editCourse";
    }


    @RequestMapping("/selectCourse")
    @ResponseBody
    public  List<Course> selectCourse(CourseRequestBo requestBo){
       return courseService.selectCourse();
    }

    @RequestMapping("/getOneCourse")
    @ResponseBody
    public  Course getOneCourse(Integer courseId){
        return courseService.selectOneCourse(courseId);
    }


    @RequestMapping("/deleteCourseById")
    @ResponseBody
    public  Integer deleteCourseById(Integer courseId){

        return courseService.deleteCourse(courseId);
    }


    @RequestMapping("/updateCourseById")
    @ResponseBody
    public  Integer updateCourseById(Course  course){

        return courseService.updateCourse(course);
    }

    @RequestMapping("/addCourseBy")
    @ResponseBody
    public   Course addCourseBy(Course  course){

        return courseService.addCourse(course);
    }


    @RequestMapping("/selectOneCourse")
    @ResponseBody
    public  String selectOneCourse(){
        Integer courseId = 6;
        Course  courseOne  = courseService.selectOneCourse(courseId);
        return courseOne.toString();
    }



    @RequestMapping("/updateCourse")
    public  String updateCourse( Course course){
        String resuleString = "";
        try{
            Integer result = courseService.updateCourse(course);
            if(result>0)
                resuleString =   "更新成功, 更新ID:  "+course.getCourseId();
            else
                resuleString =   "更新失败";
        }catch (Exception e){
            System.out.println(e.getMessage());
            resuleString =   "更新失败 : "+e.getMessage();
        }
        System.out.println(resuleString);
        return "redirect:/toCourseList";
    }
    @RequestMapping("/selectForCourseList")
    @ResponseBody
    public  ResultCourseVo selectForCourseList(CourseRequestBo requestBo){
        ResultCourseVo resultCourseVo  = new ResultCourseVo();
        System.out.println("  RequestBo   :   "+requestBo.toString());
        requestBo.setPageNum((requestBo.getPageNum()-1)*requestBo.getPageSize());
        resultCourseVo.setList(courseService.selectForCourseList(requestBo));
        resultCourseVo.setTotal(courseService.selectCountCourse(requestBo));
        return resultCourseVo;
    }



}
